package com.roadjava.rbac.controller;

import com.roadjava.rbac.bean.entity.HistoryDO;
import com.roadjava.rbac.bean.entity.HistoryEQ;
import com.roadjava.rbac.bean.res.Result;
import com.roadjava.rbac.security.RateLimitService;
import com.roadjava.rbac.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 29718
 */
@RestController
@RequestMapping("/histories")
public class HistoryController {

    @Autowired
    private HistoryService historyService;

    // 引入 RateLimitService
    private final RateLimitService rateLimitService = new RateLimitService();

    // 添加历史记录
    @PostMapping
    public Result<HistoryEQ> addHistory(@RequestBody HistoryEQ history) {
        // 假设 LikedEQ 中有用户 ID
        String userId = history.getUserId().toString();
        if (!rateLimitService.isAllowed(userId)) {
            // 返回429 Too Many Requests状态码
            return Result.buildFailure("429");
        }

        // 检查是否存在相同的历史记录
        Boolean exists = historyService.checkHistoryExists(history.getResourceId(), history.getUserId());
        if (exists) {
            // 如果历史记录已存在，返回409 Conflict
            return Result.buildSuccess();
        }
        // 通过Service层添加历史记录
        historyService.addHistory(history);
        // 返回创建的历史记录对象
        return Result.buildSuccess(history);
    }

    // 删除历史记录
    @DeleteMapping
    public Result removeHistory(@RequestParam Integer resourceId, @RequestParam Long userId, @RequestParam Integer historiesStatus) {
        // 假设通过 Id 获取用户 ID
        String id = userId.toString();
        if (!rateLimitService.isAllowed(id)) {
            // 返回429 Too Many Requests状态码
//            return ResponseEntity.status(429).build();
            return Result.buildFailure("429");
        }

        // 通过Service层删除点赞，使用资源ID和点赞ID
        historyService.removeHistory(resourceId, userId, historiesStatus);
        // 返回204状态，表示删除成功
        return Result.buildSuccess("删除成功");
    }

    // 查询用户的所有观看记录
    @GetMapping("/{userId}")
    public Result<List<HistoryDO>> getUserHistories(@PathVariable Long userId) {
        // 通过Service层查询用户所有点赞
        List<HistoryDO> histories = historyService.getUserHistory(userId);
        // 返回点赞列表
        return Result.buildSuccess(histories);
    }

//    // 查看某个资源的点赞总数
//    @GetMapping("/count")
//    public ResponseEntity<Long> getHistoryCount(@RequestParam Long resourceId) {
//        // 通过Service层查询点赞总数
//        Long count = historyService.getHistoryount(resourceId);
//        // 返回点赞总数
//        return ResponseEntity.ok(count);
//    }

    // 检查用户是否观看
    @GetMapping("/check")
    public Result<Boolean> checkUserHistory(@RequestParam Long userId, @RequestParam Long resourceId) {
        // 通过Service层检查用户是否点赞
        boolean history = historyService.checkUserHistory(userId, resourceId);
        // 返回是否点赞的结果
        return Result.buildSuccess(history);
    }
}
